package top.iforum.bbs.modules.front.controller;

import com.baomidou.mybatisplus.plugins.Page;
import top.iforum.bbs.common.bean.ResponseError;
import top.iforum.bbs.common.constant.DataBaseConstant;
import top.iforum.bbs.modules.posts.entity.Posts;
import top.iforum.bbs.modules.sys.entity.Organization;
import top.iforum.bbs.modules.sys.entity.User;
import top.iforum.bbs.modules.sys.service.IOrganizationService;
import top.iforum.bbs.modules.sys.service.ITeacherService;
import top.iforum.bbs.modules.sys.service.IUserService;
import top.iforum.bbs.utils.UserUtils;
import top.iforum.common.http.Response;
import top.iforum.common.mvc.annotation.ViewPrefix;
import top.iforum.common.mvc.controller.BaseController;
import top.iforum.common.mybatis.mvc.wrapper.EntityWrapper;
import top.iforum.common.utils.CookieUtils;
import top.iforum.common.utils.ServletUtils;
import top.iforum.common.utils.jcaptcha.JCaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 *
 * @author 张娜
 * @since 1.0
 */
@RestController("TeacherController")
@RequestMapping("user/teacher")
@ViewPrefix("modules/front/user")
public class TeacherController extends BaseController {

    @Autowired
    private ITeacherService teacherService;
    @Autowired
    private IOrganizationService organizationService;

    /**
     * 教师用户中心班级管理菜单
     * @param model
     * @return
     */
    @GetMapping("")
    public ModelAndView show(Model model) {
        model.addAttribute("currentUserRole", User.TEACHER);
        model.addAttribute("currentUserMenu","teacher");
        return displayModelAndView("teacher");
    }

    @GetMapping(value = "gradeList")
    private Response gradeList(@RequestParam("page") Integer page,
                                   @RequestParam("limit") Integer limit) throws IOException {
        Response response = Response.ok();
        EntityWrapper<Organization> entityWrapper = new EntityWrapper<>(Organization.class);
        entityWrapper.eq("create_by", UserUtils.getUser().getId());

        Page<Organization> organizationPage = new com.baomidou.mybatisplus.plugins.Page<Organization>(
                page, limit);
        organizationPage = organizationService.selectPage(organizationPage, entityWrapper);
        response.put("count",organizationPage.getTotal());
        response.putList("data",organizationPage.getRecords());
        return response;
    }

    /**
     * 教师班级管理页面
     * @return
     */
    @PostMapping("class")
    public Response addClass(Organization o,
                             @RequestParam(value = "imagecode", required = false) String imagecode,
                             HttpServletRequest request) {
        if (!JCaptcha.validateResponse(request, imagecode)) {
            return Response.error(ResponseError.NORMAL_ERROR, "请输入正确的图形码");
        }

        try {
            organizationService.insert(o);
        }catch (Exception e) {
            return Response.error("添加失败");
        }
        return Response.ok("添加成功");
    }
}
